/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.qysw.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.bean.UserBean;
import com.shwread.android.qysw10000204.R;
import com.shwread.http.base.TextHttpResponseHandler;
import com.shwread.httpsdk.http.callback.BaseAction;
import com.tgx.tina.android.task.ATaskService;

import org.apache.http.Header;

import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.UserDao;
import logic.util.Util;


/**
 * @Project： Lqyd_Android
 * @Title：SystemNotiActivity
 * @Description：
 * @Author：wayne
 * @Date：2014年8月22日下午3:47:28
 */
public class SystemNotiActivity
        extends
        BaseSimpleActivity
        implements
        OnClickListener {
    private TextView tvTitle;
    private TextView tvNotice;
    private Button btnBack;

    private String mStrTitle;
    private String mStrCotent;

    /**
     * 应用是否退出
     */
    private boolean isAppExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_notice);

        mAService = new ATaskService();
        mAService.startAService(this);

        if (getIntent().hasExtra(DefaultConsts.message_send_id)) {
            if (Util.isEmpty(Const.phone_number)) {
                isAppExit = true;
                UserBean user = UserDao.getInstance(this).getLastLoginUser();
                Const.phone_number = user.getAccount();
                Const.login_key = user.getKey();
            }
            final int msgViewId = getIntent().getIntExtra(DefaultConsts.message_send_id, -1);
//			new ReadMsgBySendIdAction(this, String.valueOf(msgViewId), new MyAction()).start();
            NewActionHelp.readMsgBySendId(this, String.valueOf(msgViewId), new MyHandler());
        }
        initData();
        initView();
        setListener();
    }

    private class MyHandler extends TextHttpResponseHandler {

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            if (isAppExit) Const.clear();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            if (isAppExit) Const.clear();
        }

    }

    private class MyAction extends BaseAction {
        @Override
        public void OK(Object obj) {
            if (isAppExit) Const.clear();
        }

        @Override
        protected void onError() {
            super.onError();
            if (isAppExit) Const.clear();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAService != null) {
            mAService.stopAService();
        }
    }

    void initData() {
        mStrTitle = getIntent().getStringExtra(DefaultConsts.title_s);
        mStrCotent = getIntent().getStringExtra(DefaultConsts.message_content_s);
    }

    void initView() {
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        tvTitle.setText(mStrTitle);
        tvNotice = (TextView) findViewById(R.id.system_notice);
        tvNotice.setText(mStrCotent);
        btnBack = (Button) findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
    }

    void setListener() {
        btnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_header_back:
                onBackPressed();
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (!existMainActivity()) {
            startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
        }
    }
}
